package com.erp.stock.controller;

import com.erp.common.ResponseMessage;
import com.erp.common.controller.BaseController;
import com.erp.stock.entity.RawMaterial;
import com.erp.stock.service.RawMaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("原材料信息接口")
@RestController
@RequestMapping("rawMeterial")
public class RawMeterialController implements BaseController<RawMaterial> {

    @Autowired
    RawMaterialService rawMaterialService;

    @ApiOperation("新增原材料信息")
    @Override
    @PostMapping("/add")
    public ResponseMessage add(RawMaterial entity) {
        if (entity.getName().isEmpty()){
            return ResponseMessage.fail("未填入原材料名");
        } else if (entity.getSupplierId() == null) {
            return ResponseMessage.fail("未选择供货商");
        }
        try {
            rawMaterialService.add(entity);
        } catch(Exception e) {
            e.printStackTrace();
            return ResponseMessage.fail("请重试");
        }
        return ResponseMessage.ok();
    }

    @ApiOperation("返回所有原材料信息")
    @GetMapping("/list")
    public ResponseMessage list(){
        List<RawMaterial> all = rawMaterialService.findAll();
        if (all.size()>0){
            return new ResponseMessage(all);
        } else{
            return ResponseMessage.fail("无相关原材料信息");
        }
    }

    @ApiOperation("删除原材料信息（支持多选）")
    @PostMapping("/list/delete")
    @Override
    public ResponseMessage deleteSome(@RequestParam(value = "idArray[]")Long[] ids) {
        if (rawMaterialService.deleteIds(ids)) {
            return ResponseMessage.ok();
        }
        return ResponseMessage.fail("删除失败，请重试");
    }

    @ApiOperation("更新原材料信息")
    @PostMapping("/update")
    @Override
    public ResponseMessage update(RawMaterial entity) {
        if (rawMaterialService.update(entity)) {
            return ResponseMessage.ok();
        }
        return ResponseMessage.fail("更新失败，请重试");
    }

    @ApiOperation("分页展示原材料信息")
    @GetMapping("/list/{page}/{size}")
    @Override
    public ResponseMessage listPage(@PathVariable("page") int page, @PathVariable("size") int size) {
        List<RawMaterial> rawMaterials = rawMaterialService.listPage(page, size);
        if (rawMaterials.size() == 0){
            return ResponseMessage.fail("无相关原材料信息");
        }
        return new ResponseMessage(rawMaterials);
    }

    @ApiOperation("分页展示原材料信息（支持多选）")
    @Override
    @GetMapping("/list/name/{page}/{size}")
    public ResponseMessage listPageLike(@PathVariable("page") int page, @PathVariable("size") int size, String column) {
        List<RawMaterial> rawMaterials = rawMaterialService.listPageLike(page, size, column);
        if (rawMaterials.size() == 0){
            return ResponseMessage.fail("无相关原材料信息");
        }
        return new ResponseMessage(rawMaterials);
    }

}
